package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.sms.mapper.AuthorityMapper;
import cn.com.allunion.sms.pojo.Authority;
import cn.com.allunion.sms.pojo.AuthorityExample;
import cn.com.allunion.sms.service.mybatis.AuthorityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 短信平台管理员 业务逻辑接口 实现类
 * Created by YangJie on 2016/4/18.
 */
@Service
@Transactional
public class AuthorityServiceImpl implements AuthorityService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorityServiceImpl.class) ;

    @Autowired
    AuthorityMapper authorityMapper ;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Authority> findAll() {
        AuthorityExample example = new AuthorityExample() ;
        return authorityMapper.selectByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Authority findByAuthorityString(String authorityString) {
        AuthorityExample example = new AuthorityExample();
        AuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andAuthorityStringEqualTo(authorityString) ;
        List<Authority> authorities = authorityMapper.selectByExample(example);
        if (null != authorities && authorities.size()>0) {
            return authorities.get(0) ;
        }
        return null;
    }

    @Override
    public boolean save(Authority authority) {
        int result = authorityMapper.insert(authority) ;
        if (result > 0) {
            return true ;
        }
        return false;
    }
}
